package logic

import (
	"app-user/common/xerr"
	"app-user/models"
	"context"
	"errors"

	"app-user/protos"
	"app-user/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CheckUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCheckUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckUserLogic {
	return &CheckUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CheckUser 检验用户
func (l *CheckUserLogic) CheckUser(in *protos.NamePassRequest) (*protos.User, error) {
	user, err := l.svcCtx.UserModel.FindOneByName(l.ctx, in.Name)
	if err != nil && !errors.Is(err, models.ErrNotFound) {
		return nil, xerr.New(xerr.UserGetError)
	}
	if user.Password != in.Password {
		return nil, xerr.New(xerr.UserPassError)
	}
	return &protos.User{Id: user.Id, Name: user.Name}, nil
}
